/*
 * Ant Group
 * Copyright (c) 2004-2024 All Rights Reserved.
 */
package com.alipay.zmdepositdemo.util;

import com.alipay.api.AlipayApiException;
import com.alipay.api.AlipayClient;
import com.alipay.api.AlipayConfig;
import com.alipay.api.AlipayRequest;
import com.alipay.api.AlipayResponse;
import com.alipay.api.DefaultAlipayClient;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.stereotype.Component;

import javax.annotation.PostConstruct;
import java.util.HashMap;
import java.util.Map;

/**
 * AlipaySDK工具类
 *
 * @author zhangjian
 * @version AlipaySdkUtil.java, v 0.1 2024年06月14日 11:22 zhangjian
 * @see .src/main/resources/application.yaml
 */
@Component
@ConfigurationProperties(prefix = "alipay")
public class AlipaySdkUtil {

    /**
     * AlipaySDK客户端
     */
    private AlipayClient alipayClient;

    /**
     * 小程序Id
     * 请在支付宝开放平台申请、查看
     *
     * @see <a href="https://open.alipay.com/develop/manage">...</a>
     */
    private String appId;

    /**
     * 私钥
     */
    private String privateKey;

    /**
     * 支付宝公钥
     */
    private String alipayPublicKey;

    /**
     * 商户Id
     * 请在支付宝商家平台查看
     *
     * @see <a href="https://b.alipay.com/page/home">...</a>
     */
    private String pid;

    /**
     * 是否使用openId
     *
     * @see <a href="https://opendocs.alipay.com/mini/0ai2i6">openId简介</a>
     */
    private boolean useOpenId;

    /**
     * openId
     * 使用OpenId时必填（alipay.openid）
     */
    private String openid;

    /**
     * 用户ID，和OpenId二选一（alipay.userid）
     */
    private String userid;

    /**
     * 是否是服务商（alipay.is_isv）
     */
    private boolean isIsv;

    /**
     * 是否是服务商（alipay.is_isv）
     */
    private String appAuthToken;

    @PostConstruct
    public void init() throws AlipayApiException {
        // 初始化v2 SDK
        this.alipayClient = new DefaultAlipayClient(getAlipayConfig());
    }

    public <T extends AlipayResponse> T execute(AlipayRequest<T> request) throws AlipayApiException {
        if (isIsv) {
            return alipayClient.execute(request, null, appAuthToken);
        }
        return alipayClient.execute(request);
    }

    private AlipayConfig getAlipayConfig() {
        AlipayConfig alipayConfig = new AlipayConfig();
        alipayConfig.setServerUrl("https://openapi.alipay.com/gateway.do");
        alipayConfig.setAppId(appId);
        alipayConfig.setPrivateKey(privateKey);
        alipayConfig.setAlipayPublicKey(alipayPublicKey);
        alipayConfig.setFormat("json");
        alipayConfig.setCharset("UTF-8");
        alipayConfig.setSignType("RSA2");
        Map<String, String> headers = new HashMap<>();
        headers.put("zm-deposit-demo", "demo-0.0.1");
        alipayConfig.setCustomHeaders(headers);
        return alipayConfig;
    }

    /**
     * Getter method for property <tt>pid</tt>.
     *
     * @return property value of pid
     */
    public String getPid() {
        return pid;
    }

    /**
     * Setter method for property <tt>pid</tt>.
     *
     * @param pid value to be assigned to property pid
     */
    public void setPid(String pid) {
        this.pid = pid;
    }

    /**
     * Getter method for property <tt>appId</tt>.
     *
     * @return property value of appId
     */
    public String getAppId() {
        return appId;
    }

    /**
     * Setter method for property <tt>appId</tt>.
     *
     * @param appId value to be assigned to property appId
     */
    public void setAppId(String appId) {
        this.appId = appId;
    }

    /**
     * Getter method for property <tt>openid</tt>.
     *
     * @return property value of openid
     */
    public String getOpenid() {
        return openid;
    }

    /**
     * Setter method for property <tt>openid</tt>.
     *
     * @param openid value to be assigned to property openid
     */
    public void setOpenid(String openid) {
        this.openid = openid;
    }

    /**
     * Getter method for property <tt>userid</tt>.
     *
     * @return property value of userid
     */
    public String getUserid() {
        return userid;
    }

    /**
     * Setter method for property <tt>userid</tt>.
     *
     * @param userid value to be assigned to property userid
     */
    public void setUserid(String userid) {
        this.userid = userid;
    }

    /**
     * Getter method for property <tt>useOpenId</tt>.
     *
     * @return property value of useOpenId
     */
    public boolean isUseOpenId() {
        return useOpenId;
    }

    /**
     * Setter method for property <tt>useOpenId</tt>.
     *
     * @param useOpenId value to be assigned to property useOpenId
     */
    public void setUseOpenId(boolean useOpenId) {
        this.useOpenId = useOpenId;
    }

    /**
     * Getter method for property <tt>isIsv</tt>.
     *
     * @return property value of isIsv
     */
    public boolean isv() {
        return isIsv;
    }

    /**
     * Setter method for property <tt>isIsv</tt>.
     *
     * @param isv value to be assigned to property isIsv
     */
    public void setIsv(boolean isv) {
        isIsv = isv;
    }

    /**
     * Getter method for property <tt>appAuthToken</tt>.
     *
     * @return property value of appAuthToken
     */
    public String getAppAuthToken() {
        return appAuthToken;
    }

    /**
     * Setter method for property <tt>appAuthToken</tt>.
     *
     * @param appAuthToken value to be assigned to property appAuthToken
     */
    public void setAppAuthToken(String appAuthToken) {
        this.appAuthToken = appAuthToken;
    }

    /**
     * Getter method for property <tt>privateKey</tt>.
     *
     * @return property value of privateKey
     */
    public String getPrivateKey() {
        return privateKey;
    }

    /**
     * Setter method for property <tt>privateKey</tt>.
     *
     * @param privateKey value to be assigned to property privateKey
     */
    public void setPrivateKey(String privateKey) {
        this.privateKey = privateKey;
    }

    /**
     * Getter method for property <tt>alipayPublicKey</tt>.
     *
     * @return property value of alipayPublicKey
     */
    public String getAlipayPublicKey() {
        return alipayPublicKey;
    }

    /**
     * Setter method for property <tt>alipayPublicKey</tt>.
     *
     * @param alipayPublicKey value to be assigned to property alipayPublicKey
     */
    public void setAlipayPublicKey(String alipayPublicKey) {
        this.alipayPublicKey = alipayPublicKey;
    }
}